package com.qianya.oa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianya.oa.domain.SysUserMessage;
import com.qianya.oa.dto.websocket.WebsocketMessageDto;
import com.qianya.oa.mapper.SysUserMessageMapper;
import com.qianya.oa.service.SysUserMessageService;
import com.qianya.oa.vo.websocket.SysUserMessageVo;
import com.qianya.oa.websocket.MyWebSocketHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.annotation.Tainted;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysUserMessageServiceImpl implements SysUserMessageService {

    @Resource
    private SysUserMessageMapper sysUserMessageMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return sysUserMessageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SysUserMessage record) {
        return sysUserMessageMapper.insert(record);
    }

    @Override
    public int insertSelective(SysUserMessage record) {
        return sysUserMessageMapper.insertSelective(record);
    }

    @Override
    public SysUserMessage selectByPrimaryKey(Long id) {
        return sysUserMessageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SysUserMessage record) {
        return sysUserMessageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SysUserMessage record) {
        return sysUserMessageMapper.updateByPrimaryKey(record);
    }

    @Override
    public int batchInsert(List<SysUserMessage> list) {
        return sysUserMessageMapper.batchInsert(list);
    }

    @Override
    public PageInfo<SysUserMessageVo> getAndReadMessage(Long uid, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<SysUserMessageVo> messageVoList = sysUserMessageMapper.selectListByUId(uid);
        if (messageVoList.size() > 0) {
            List<Long> messageIdList = messageVoList.stream().map(SysUserMessageVo::getId).collect(Collectors.toList());
            //将不是自己发起的未读消息转已读
            if (messageIdList.size() > 0) {
                sysUserMessageMapper.setUnreadToRead(messageIdList);
            }
        }
        return new PageInfo<>(messageVoList);
    }

    /**
     * 发送后马上通知ws拉取，会存在事务问题，设置传播级别为不支持事务，挂起其他事务
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public Boolean sendAndSaveMessage(Long toUserId, String toUserName, Integer messageType, String messageTitle, String messageContent) {
        //存储消息
        SysUserMessage sysUserMessage = new SysUserMessage();
        sysUserMessage.setMessageTitle(messageTitle);
        sysUserMessage.setUserId(toUserId);
        sysUserMessage.setUserName(toUserName);
        sysUserMessage.setSendTime(new Date());
        sysUserMessage.setMessageType(messageType);
        sysUserMessage.setMessageContent(messageContent);
        int i1 = sysUserMessageMapper.insertSelective(sysUserMessage);
        if (i1 > 0) {
            //推送ws消息
            WebsocketMessageDto websocketMessage = new WebsocketMessageDto();
            websocketMessage.setToUserId(toUserId);
            websocketMessage.setMessageType(messageType);
            websocketMessage.setMessageContent(messageContent);
            MyWebSocketHandler.sendTo(websocketMessage);
        }
        return i1 > 0;
    }

    @Override
    public int getUnreadCount(Long uid) {
        return sysUserMessageMapper.selectUnreadCount(uid);
    }

}





